import GViewBase from "../../../Core/GView/GViewBase";
import { BOXPREVIEW, ITEM_DETAIL_FLAG, VIPBOXTYPE } from "../../Common/Define";
import { Res } from "../../Common/UIResources";
import RJXItem from "../../Data/RunTime/RunRaw/RJXItem";
import JXItem from "./JXItem";

const { ccclass, property, menu } = cc._decorator;

@ccclass @menu('View/Common/PreviewCtrl')
export default class PreviewCtrl extends GViewBase {

    @property({ type: cc.Node, tooltip: "奖励预览" }) jxLayer: cc.Node = null;


    onGStart(data: any, index: number) {

        switch (index) {
            case BOXPREVIEW.FIRSTBOX:
            case BOXPREVIEW.DAYBOX:
                {
                    let boxData: SVipDataRaw = data;
                    this.init(boxData, index);
                    break;
                }
            case BOXPREVIEW.LEVELBOX:
                {
                    let boxData: SStoryDataRaw = data;
                    this.initBox(boxData);
                    break;
                }
            case BOXPREVIEW.OFFICEBOX:
                {
                    let boxData: SOfficerDataRaw = data;
                    this.initOfficeBox(boxData);
                    break;
                }
            default:
                break;
        }
    }

    init(data: SVipDataRaw, index) {
        this.assetImpl.prefab(Res.prefab.item.jxItem, (prefab: cc.Prefab) => {
            if (index == VIPBOXTYPE.DAYBOX) {
                data.recycle.forEach((value) => {
                    let comp = this.assetImpl.addGChild<JXItem>(prefab);
                    comp.node.parent = this.jxLayer;
                    let item = new RJXItem(value);
                    comp.setView(item, ITEM_DETAIL_FLAG.BAG | ITEM_DETAIL_FLAG.NUMPOS_ITEM_BC | ITEM_DETAIL_FLAG.NOT_TOUCH | ITEM_DETAIL_FLAG.SHOWLIGHT | ITEM_DETAIL_FLAG.LIGHT);
                })
            } else {
                data.recycleUp.forEach((value) => {
                    let comp = this.assetImpl.addGChild<JXItem>(prefab);
                    comp.node.parent = this.jxLayer;
                    let item = new RJXItem(value);
                    comp.setView(item, ITEM_DETAIL_FLAG.BAG | ITEM_DETAIL_FLAG.NUMPOS_ITEM_BC | ITEM_DETAIL_FLAG.NOT_TOUCH | ITEM_DETAIL_FLAG.SHOWLIGHT | ITEM_DETAIL_FLAG.LIGHT);
                })
            }
        })
    }

    initBox(data: SStoryDataRaw) {
        this.assetImpl.prefab(Res.prefab.item.jxItem, (prefab: cc.Prefab) => {
            data.sectionBox.forEach((value) => {
                let comp = this.assetImpl.addGChild<JXItem>(prefab);
                comp.node.parent = this.jxLayer;
                let item = new RJXItem(value);
                comp.setView(item, ITEM_DETAIL_FLAG.BAG | ITEM_DETAIL_FLAG.NUMPOS_ITEM_BC | ITEM_DETAIL_FLAG.NOT_TOUCH | ITEM_DETAIL_FLAG.SHOWLIGHT | ITEM_DETAIL_FLAG.LIGHT);
            })
        })
    }

    initOfficeBox(data: SOfficerDataRaw) {
        this.assetImpl.prefab(Res.prefab.item.jxItem, (prefab: cc.Prefab) => {
            data.award.forEach((value) => {
                let comp = this.assetImpl.addGChild<JXItem>(prefab);
                comp.node.parent = this.jxLayer;
                let item = new RJXItem(value);
                comp.setView(item, ITEM_DETAIL_FLAG.BAG | ITEM_DETAIL_FLAG.NUMPOS_ITEM_BC | ITEM_DETAIL_FLAG.NOT_TOUCH | ITEM_DETAIL_FLAG.SHOWLIGHT | ITEM_DETAIL_FLAG.LIGHT);
            })
        })
    }

    close() {
        this.onClose();
    }
}
